package pearson.automation.forms;

import java.util.List;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pearson.automation.assets.AssetList;

import framework.elements.Button;
import framework.elements.Label;

public class BrowseForm extends BaseFormWithSidebarsAndSearch {

    /* Subject filter elements */
    private Label subjectTopFilterList = new Label(By.cssSelector("div.subject-select li > a"), "Subject filters");
    private Label listCollaptioned = new Label(By.cssSelector("div.subject-select ul"), "Subject collaption");
    private Button filterBySubject = new Button(By.linkText("Filter by Subject"), "Filter by Subject");
       
    private String featuredCollectionItemXPathTemplate = "//div[@class='section contain'][contains(.,'Featured Collection')]";
    private String popularItemsItemXPathTemplate = "//div[@class='section contain divider'][contains(.,'Popular Items')]";
    
    private String principlesOfMarketingItemXPathTemplate = "//div[@class='section contain'][contains(.,'Principles of Marketing Collection')]";
    private String popularMarketingItemXPathTemplate = "//div[@class='section contain divider'][contains(.,'Popular Marketing Items')]";
    
    private String principlesOfEconomicsItemXPathTemplate = "//div[@class='section contain'][contains(.,'Principles of Economics')]";

    public AssetList featuredCollection;
    public AssetList popularItems;
    public AssetList principlesOfMarketingItems;
    public AssetList principlesOfEconomicsItems;
    public AssetList popularMarketingItems;
    
    public BrowseForm() {
    super(By.xpath("//div[contains(.,'Filter by Subject')]"), "Main page");

    }

    /**
     * Get subject filters on the browse page
     * 
     * @return String array of the available filters
     */
    public String[] getTopSubjectFilters() {
    subjectTopFilterList.waitAndAssertIsPresent();
    List<WebElement> list = browser.getDriver().findElements(subjectTopFilterList.getLocator());
    String[] result = new String[list.size()];
    for (int i = 0; i < result.length; i++) {
        result[i] = list.get(i).getText();
    }
    return result;
    }

    public void filterBySubject(String subject) {
    Button filter = new Button(By.linkText(subject), subject);
    filter.clickAndWait();
    new BrowseForm();
    }

    /**
     * 
     * @param collapse
     *            - should be collapsed or expanded
     */
    public void collapseSubjectFilter(boolean collapse) {
    listCollaptioned.waitAndAssertIsPresent();
    for (int i = 0; i < 3; i++) {
        if (listCollaptioned.isCollapsed() != collapse) {
        filterBySubject.click();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        }
    }
    Assert.assertSame("Subject filter is in a wrong state.", listCollaptioned.isCollapsed(), collapse);
    }
    
    public void declareBrowseLists() {
        featuredCollection = new AssetList(featuredCollectionItemXPathTemplate, "//li[contains(@class, 'srl-item')]");
        popularItems = new AssetList(popularItemsItemXPathTemplate, "//li[contains(@class, 'srl-item')]");
    }
    
    public void declareMarketingLists() {
        principlesOfMarketingItems = new AssetList(principlesOfMarketingItemXPathTemplate, "//li[contains(@class, 'srl-item')]");
        popularMarketingItems = new AssetList(popularMarketingItemXPathTemplate, "//li[contains(@class, 'srl-item')]");
    }
    
    public void declareEconomicLists() {
        principlesOfEconomicsItems = new AssetList(principlesOfEconomicsItemXPathTemplate, "//li[contains(@class, 'srl-item')]");
    }

}
